//
//  SLHttpClient.swift
//  SLBlog
//
//  Created by 盼 on 2017/12/8.
//  Copyright © 2017年 pan. All rights reserved.
//

import UIKit
import AFNetworking

enum SLHttpMethod {
    case Get
    case Post
}

class SLHttpClient: AFHTTPSessionManager {

    static let shared = SLHttpClient()
    
    //注意escaping的用法，非逃逸闭包
    
    /// 网络请求
    ///
    /// - Parameters:
    ///   - method: 请求方法 Get／Post
    ///   - urlStr: URL地址
    ///   - params: 网络请求参数
    ///   - success: 请求成功的回调闭包
    ///   - failure: 请求失败的回调闭包
    
    func request_2(method: SLHttpMethod, urlStr: String, params: [String: Any]?, success:@escaping (_ task: URLSessionDataTask, _ json: Any?)->(),failure:@escaping (_ task: URLSessionDataTask?,_ error: Error)->()) {
        
        var paras = params
//        paras?["access_token"] = "2.00V9PLvC0wFgu7badc7cf0c7FqwhfD" as AnyObject
        
        if paras == nil {
            paras = [String: Any]()
        }
        paras?["access_token"] = "2.00V9PLvC0wFgu7badc7cf0c7FqwhfD"
        
        
        get(urlStr, parameters: paras, progress: nil, success: { (task, json) in
            
            success(task, json)
            
        }) { (task, error) in
            
            failure(task,error)
            
//            if let（task?.response as?HTTPURLResponse)?.statusCode == 403 {
//                //
//                print("token过期，需要重新登录")
//            }
        }
    }
}
